<?php
function coreupdatecache($cachename = '', $designate = array()) {
	global $db, $tablepre, $settings, $codekey;
	if(empty($cachename) || $cachename == 'categories') {
		$return = array();
		$_categorytmp = array();
		$sql = "SELECT * FROM {$tablepre}_categories ORDER BY id";
		$query = $db->query($sql);
		while($category = $db->fetch_array($query)) {
			$category['subcategories'] = array();
			$return[$category['id']] = $category;
			if(!empty($_categorytmp[$category['id']])) {
				$return[$category['id']]['subcategories'] = $_categorytmp[$category['id']];
				unset($_categorytmp[$category['id']]);
			}
			if($category['categoryup'] != 0) {
				if(empty($return[$category['categoryup']])) {
					$_categorytmp[$category['categoryup']][] = $category['id'];
				} else {
					$return[$category['categoryup']]['subcategories'][] = $category['id'];
				}
			}
		}
		writetocache('categories', $return);
		if($cachename != '') coreupdatecache('categoriesselect');
	}
	if(empty($cachename) || $cachename == 'categoriesselect') {
		$select = rendercategoryselect();
		writetocache('categoriesselect', $select);
	}
	if(empty($cachename) || $cachename == 'sections') {
		$return = array();
		$sql = "SELECT * FROM {$tablepre}_sections";
		$query = $db->query($sql);
		while($var = $db->fetch_array($query)) {
			$return[$var['id']] = $var;
		}
		writetocache('sections', $return);
	}
	if(empty($cachename) || $cachename == 'templates') {
		global $template_path;
		$return = array();
		$dir = AK_ROOT.'templates/'.$template_path.'/';
		$dh  = opendir($dir);
		while (false !== ($filename = readdir($dh))) {
			if($filename != '.' && $filename != '..' && substr($filename, 0, 1) == ',') {
				$return[] = substr($filename, 1);
			}
		}
		sort($return);
		writetocache('templates', $return);
	}
	if(empty($cachename) || $cachename == 'globalvariables') {
		$return = array();
		$sql = "SELECT * FROM {$tablepre}_variables ORDER BY variable";
		$query = $db->query($sql);
		while($var = $db->fetch_array($query)) {
			$return[$var['variable']] = $var['value'];
		}
		writetocache('globalvariables', $return);
	}
	if(empty($cachename) || $cachename == 'settings') {
		$return = array();
		$sql = "SELECT * FROM {$tablepre}_settings";
		$query = $db->query($sql);
		while($var = $db->fetch_array($query)) {
			$return[$var['variable']] = $var['value'];
		}
		writetocache('settings', $return);
	}
	if((empty($cachename) || $cachename == 'infos')) {
		$return = array();
		$items = $db->get_by('COUNT(*)', 'items', 'category>0');
		$pvs = $db->get_by('SUM(pageview)', 'items');
		$editors = $db->get_field("SELECT COUNT(*) FROM {$tablepre}_admins WHERE freeze=0");
		$attachmentsizes = $db->get_field("SELECT SUM(filesize) FROM {$tablepre}_attachments");
		$attachments = $db->get_field("SELECT COUNT(*) FROM {$tablepre}_attachments");
		$return = array(
			'items' => $items,
			'pvs' => $pvs,
			'editors' => $editors,
			'attachmentsizes' => $attachmentsizes,
			'attachments' => $attachments
		);
		writetocache('infos', $return);
	}
	if(empty($cachename) || $cachename == 'crons') {
		$return = array();
		$query = $db->query("SELECT * FROM {$tablepre}_crons");
		while($var = $db->fetch_array($query, 1)) {
			$return[$var['id']] = $var;
			$return[$var['id']]['nexttime'] = cronnexttime($var);
		}
		writetocache('crons', $return);
	}
	if(empty($cachename) || $cachename == 'spiders') {
		$return = array();
		$query = $db->query("SELECT * FROM {$tablepre}_spiders");
		while($spider = $db->fetch_array($query)) {
			$_v = unserialize($spider['data']);
			$spider = array_merge($spider, $_v);
			unset($spider['data']);
			$return[$spider['id']] = $spider;
		}
		writetocache('spiders', $return);
	}
	if(empty($cachename) || $cachename == 'spiderrules') {
		$query = $db->list_by('*', 'spiderrules');
		$return = array();
		while($spiderrule = $db->fetch_array($query)) {
			$ruleid = $spiderrule['id'];
			$spiderrule = ak_unserialize($spiderrule['data']);
			for($i = 1; $i <= 20; $i ++) {
				$spiderrule['field'.$i.'_start'] = get_rule_field($spiderrule['field'.$i], 0);
				$spiderrule['field'.$i.'_end'] = get_rule_field($spiderrule['field'.$i], 1);
				unset($spiderrule['field'.$i]);
			}
			$spiderrule['id'] = $ruleid;
			$return[$ruleid] = $spiderrule;
		}
		writetocache('spiderrules', $return);
	}
	if(empty($cachename) || $cachename == 'plugins') {
		$paths = readpathtoarray(AK_ROOT.'/plugins');
		$return = array();
		foreach($paths as $path) {
			if(is_dir($path)) continue;
			if(!is_readable($path)) continue;
			if(fileext($path) == 'php') $return[] = calfilenamefromurl($path);
		}
		writetocache('plugins', $return);
	}
	if(empty($cachename) || $cachename == 'keywords') {
		$query = $db->list_by('*', 'items', 'category=-1', 'orderby desc');
		$_keywords = '';
		while($keyword = $db->fetch_array($query)) {
			$_k = $keyword['title']."\t".$keyword['aimurl']."\t".$keyword['digest']."\n";
			if(frequence($_k, "\t") > 2) continue;
			$_keywords .= $_k;
		}
		writetofile($_keywords, AK_ROOT.'cache/'.$codekey.'_cache_keywords.txt');
	}
	if(empty($cachename) || $cachename == 'modules') {
		$query = $db->list_by('*', 'modules');
		$return = array();
		while($module = $db->fetch_array($query)) {
			$module['data'] = ak_unserialize($module['data']);
			$return[$module['id']] = $module;
		}
		writetocache('modules', $return);
	}
}

function writetocache($script, $cachedata = array()) {
	global $codekey;
	$data = serialize($cachedata);
	if(1) {
		return writetofile($data, AK_ROOT.'cache/'.$codekey."_cache_{$script}.php");
	}
}
?>